/*************************************************************************
    > File Name: exercise3-A.cpp
    > Author: Botao
    > Mail: peng.botao@aliyun.com 
    > Created Time: Thu 11 Nov 2021 03:42:01 PM CST
 ************************************************************************/
#include <stdio.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>

#define MB 1024*1024
#define KB 1024

int main(){
    
    int i, pid;
    char temp;
    int fd=-1;
    char * filemap_buf;
    char command_smaps[100];
    char command_status[100];
    char command_meminfo[100];
    char command_maps[100];

    pid=getpid();
    sprintf(command_smaps,"cat /proc/%d/smaps >smaps-check", pid);
    sprintf(command_status,"cat /proc/%d/status >status-check", pid);
    sprintf(command_meminfo,"cat /proc/meminfo >meminfo-check");
    sprintf(command_maps,"cat /proc/%d/maps >maps-check", pid); 
    filemap_buf=(char *)malloc(640*MB);


    printf("allocated\n");

    system(command_maps);
    system(command_smaps);
    system(command_status);
    system(command_meminfo);
    getchar();    //checkpoint 1 allocation

    for(i=0;i<640*MB;i+=4096)
        filemap_buf[i]=temp;

    printf("write finish\n");
    system(command_smaps);
    system(command_status);
    system(command_meminfo);
    getchar();    //checkpoint 2 write finish/make the pageframes dirty 

    //run pages-blakhole-demo.c
    printf("please run exercise3-B\n");
    getchar();   //checkpoint 3 be swapped out
    system(command_smaps);
    system(command_status);
    system(command_meminfo);
    getchar();

    munmap(filemap_buf, 8*MB);
    return 0;

}
